package cn.com.darly.videoandroidpm.ui.biz;

import android.app.Dialog;

import com.darly.common.DLog;
import com.darly.common.NetUtils;
import com.darly.common.retrofit.FrameListener;
import com.darly.common.retrofit.FramePresenter;
import com.darly.common.retrofit.FrameView;
import com.darly.common.retrofit.RxjavaRetrofitRequestUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.Map;

import cn.com.darly.videoandroidpm.base.BaseApplication;
import cn.com.darly.videoandroidpm.bean.BaseBean;
import cn.com.darly.videoandroidpm.common.retrofit.HttpInterface;
import okhttp3.RequestBody;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;


/**
 * @author zhangyh2 LoginUser 下午3:42:16 TODO 用户登录获取数据传递给了接口
 */
public class ConnectListener implements FramePresenter {
    @Override
    public void onStart(Dialog loading) {

    }

    @Override
    public void onStop(Dialog loading) {

    }

    public void disConnect(Map<String, String> params, final FrameListener<BaseBean<String>> listener) {
        if (!NetUtils.isNetworkAvailable(BaseApplication.getInstance())) {
            listener.onFaild(0, "网络异常,请检查网络");
            return;
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/x-www-form-urlencoded"), RxjavaRetrofitRequestUtil.getParamers(params, "UTF-8"));
        RxjavaRetrofitRequestUtil.getInstance().post(HttpInterface.class).postStop(body).subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<JsonObject, BaseBean<String>>() {
                    @Override
                    public BaseBean<String> call(JsonObject s) {
                        DLog.json("Func1", s.toString());
                        BaseBean root = new Gson().fromJson(s, new TypeToken<BaseBean<String>>() {
                        }.getType());
                        return root;
                    }
                })
                .subscribe(new Observer<BaseBean<String>>() {
                               @Override
                               public void onCompleted() {

                               }

                               @Override
                               public void onError(Throwable e) {
                                   listener.onFaild(1, "网络连接异常，请检查网络");
                               }

                               @Override
                               public void onNext(BaseBean<String> data) {
                                   if (data != null) {
                                       if (data.getCode() == 200) {
                                           listener.onSucces(data);
                                       } else {
                                           listener.onFaild(1, data.getMsg());
                                       }
                                   } else {
                                       listener.onFaild(0, "解析失败");
                                   }
                               }
                           }

                );
    }


    public interface ConnectInterface extends FrameView {

        void onConnectSuccess(String info);

        void onConnectFailed(String info);
    }
}
